package adv10;

import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class CreateFromZeroXml {
	//从头开始创建xml
	public static void main(String[] args) {
		Document doc=DocumentHelper.createDocument();
		Element root=doc.addElement("shop");
		root.addAttribute("name", "家乐福马鞍山路店");
		Element p=root.addElement("product");

		p.addAttribute("discount", "false");
		p.addAttribute("factoryaddr", "合肥");
		//product的子节点
		p.addElement("itemno").setText("1000");
		p.addElement("name").setText("江淮iev6");
		p.addElement("price").setText("98000");
		p.addElement("stockcount").setText("44");
		p.addElement("unit").setText("部");
		
		try {
			OutputFormat f = OutputFormat.createPrettyPrint();
			f.setEncoding("utf-8");
			XMLWriter xm = new XMLWriter(new FileWriter("products2.xml"), f);
			xm.write(doc);
			xm.close();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

}
